#include "common/common_time_util.h"
#include <chrono>
#include <sys/time.h>
#include <time.h>

int64_t Common_get_sys_time_sec() {
#ifndef USE_C_TIME
  std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds> tp =
      std::chrono::time_point_cast<std::chrono::seconds>(
          std::chrono::system_clock::now());

  auto tmp =
      std::chrono::duration_cast<std::chrono::seconds>(tp.time_since_epoch());

  return tmp.count();
#else
  printf("asdfadf\n");
  time_t systime = time(NULL);
  return systime;
#endif
}

int64_t Common_get_sys_time_msec() {
#ifndef USE_C_TIME
  std::chrono::time_point<std::chrono::system_clock, std::chrono::microseconds>
      tp = std::chrono::time_point_cast<std::chrono::microseconds>(
          std::chrono::system_clock::now());

  auto tmp = std::chrono::duration_cast<std::chrono::microseconds>(
      tp.time_since_epoch());

  return tmp.count();
#else
  struct timeval tv;
  struct timezone tz;
  int ret = gettimeofday(&tv, &tz);
  if (ret < 0) {
    return -1;
  }
  return tv.tv_sec * 1000 + tv.tv_usec / 1000;
#endif
}

std::string Common_get_sys_time() {
//
        //    struct tm {
        //        int tm_sec;    /* Seconds (0-60) */
        //        int tm_min;    /* Minutes (0-59) */
        //        int tm_hour;   /* Hours (0-23) */
        //        int tm_mday;   /* Day of the month (1-31) */
        //        int tm_mon;    /* Month (0-11) */
        //        int tm_year;   /* Year - 1900 */
        //        int tm_wday;   /* Day of the week (0-6, Sunday = 0) */
        //        int tm_yday;   /* Day in the year (0-365, 1 Jan = 0) */
        //        int tm_isdst;  /* Daylight saving time */
        //    };

//

  int64_t systime = Common_get_sys_time_sec();
  struct tm localtime;
  struct tm* temptime = localtime_r(&systime, &localtime);
  char timebuf[128] = {0};
  sprintf(timebuf, "%04d%02d%02d_%02d%02d%02d", temptime->tm_year + 1900,
          temptime->tm_mon + 1, temptime->tm_mday, temptime->tm_hour,
          temptime->tm_min, temptime->tm_sec);

  printf("timebuf:%s\n", timebuf);
  std::string tmp = timebuf;
  return tmp;
}
